package binary;

/**
 * leet-code(33) 搜索旋转排序数组
 *
 * @auther yang
 * @date 2022/3/18 14:35
 */
public class SearchRotate {

    public int search(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while (left <= right) {
            int mid = left + ((right - left) >> 1);
            if (nums[mid] == target)
                return mid;
            if (nums[mid] >= nums[left]) {
                if (nums[mid] > target && target >= nums[left])
                    right = mid - 1;
                else if (target > nums[mid] || target < nums[left])
                    left = mid + 1;
            } else if (nums[mid] < nums[left]) {
                if (nums[mid] < target && target <= nums[right])
                    left = mid + 1;
                else if (target < nums[mid] || target > nums[right])
                    right = mid - 1;
            }
        }
        return -1;
    }

}
